<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;

/**
 * 用户控制器
 *
 * @author derek <693472824@qq.com>
 */
class AdminController extends BaseController {

    /**
     * 获取用户列表
     *
     * @param string $adminName 用户名称
     * @return 返回列表
     * @author derek <693472824@qq.com>
     */
    public function index () {
        
        $admin_name = I('adminName');
        $admin_where['admin_name'] = array('like', '%'.(string)$admin_name.'%');
        
        $admin_list = M('Admin')->where($admin_where)->order('admin_id desc');
        
        $list = $this->lists($admin_list);
        $roleList = M('Role')->getField('role_id, role_name');
        
        $this->assign('adminList', $list);
        $this->assign('roleList', $roleList);
        $this->assign('adminName', $admin_name);
        $this->display();
    }

    /**
     * 增加用户以及修改用户
     *
     * @param integer $admin_id
     *            用户ID.
     * @author derek <693472824@qq.com>
     */
    public function editAction() {
        $admin_id = I('admin_id');
        
        if (! empty($admin_id)) {
            
            $adminLogic = D('Admin', 'Logic');
            $adminModel = $adminLogic->getAdminByID($admin_id);
            
            if (empty($adminModel)) {
                $this->error('获取数据失败', U('Admin/index'));
            } else {
                $this->assign('adminModel', $adminModel);
            }
        }
        
        $role_list = D('Role', 'Logic')->getRoleListByName();
        $siteList = M('Site')->getField('site_id,site_name,address');
        
        $this->assign('roleList', $role_list);
        $this->assign('siteList', $siteList);
        $this->display();
    }

    /**
     * 保存用户信息
     *
     * @author derek <lixm@my818.com>
     */
    public function saveAction() {
        
        $adminLogic = D('Admin', 'Logic');
        $result = $adminLogic->saveAction();
        
        if ($result === false) {
            $this->error('操作失败' . $adminLogic->getError());
        } else {
            $this->success('操作成功', U('Admin/index'));
        }
    }

    /**
     * 用户删除
     *
     * @param array|integer $admin_ids ID
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function del () {
        
        $admin_ids = I('admin_ids');
        if (empty($admin_ids)) {
            $this->error('参数错误');
        }
        $admin = D('Admin', 'Logic');
        $result = $admin->del($admin_ids);
        
        if ($result === false) {
            $this->error('操作失败', U('index'));
        } else {
            $this->success('操作成功！', U('index'));
        }
    }

    /**
     * 密码初始化设置
     *
     * @param int|string $admin_id
     *            用户ID,$pwd 初始密码
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function init_pwd () {
        
        $admin_id = I('admin_id');
        
        if (empty($admin_id)) {
            $this->error('参数错误');
        }
        
        $adminLogic = D('Admin', 'Logic');
        $result = $adminLogic->init_pwd($admin_id);
        
        if ($result === false) {
            $this->error('操作失败', U('index'));
        } else {
            $this->success('操作成功！'. $adminLogic->getError(), U('index'));
        }
    }
}